package dataAccessLayer.tests;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;

import dataAccessLayer.dataBase.DBManager;
import dataAccessLayer.factoryMySQL.daos.DAOsalesMySQL;
import dataAccessLayer.visualObject.VOcustomer;
import dataAccessLayer.visualObject.VOsale;

import junit.framework.TestCase;

public class TestDAOsalesMySQL extends TestCase 
{
	private DBManager manager = new DBManager(ResourceBundle.getBundle("dataAccessLayer.dataAccess").getString("localConnectionType"));
	private DAOsalesMySQL DAOsales = new DAOsalesMySQL(manager);
	
	public void testInsertSale()
	{
		VOsale vOsale= new VOsale();
		vOsale.setCustomer(1);
		vOsale.setDate("2010-05-06");
		vOsale.setSeller(1);
		vOsale.setTotalValue(15000);
		
		this.DAOsales.insertSale(vOsale);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		String date = "";
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM sales ORDER BY code DESC LIMIT 1");
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				date=resultSet.getString(2);
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		
		assertEquals(vOsale.getDate(), date);
	}
	
	public void testUpdateSale()
	{
		VOsale vOsale= new VOsale();
		vOsale.setCustomer(1);
		vOsale.setDate("2015-06-07");
		vOsale.setSeller(1);
		vOsale.setTotalValue(30000);
		vOsale.setCode(2);
		
		this.DAOsales.updateSale(vOsale);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		String date="";
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM sales where code=?");
			preparedStatement.setInt(1,vOsale.getCode());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				date = resultSet.getString(2);
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		
		assertEquals(vOsale.getDate(), date);
	}
	
	public void testRemoveSale() throws Exception
	{
		VOsale vOsale= new VOsale();
		vOsale.setCode(4);
		
		this.DAOsales.removeSale(vOsale);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		VOcustomer searchVoSale = new VOcustomer();
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM sales where code=?");
			preparedStatement.setInt(1,vOsale.getCode());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				searchVoSale.setCode(resultSet.getInt(1));
				searchVoSale.setName(resultSet.getString(2));
			}
			else
			{
				searchVoSale=null;
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		assertEquals(searchVoSale,null );
	}
	
	public void testListSales() throws Exception
	{
		ArrayList<VOsale> search = this.DAOsales.listSales();
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		
		int count=0;
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM sales");
			resultSet = preparedStatement.executeQuery();
			
			while(resultSet.next())
			{
				count++;
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(search.size(),count );
	}
	
	public void testSearchSaleByCode() throws Exception 
	{
		VOsale vOsale= new VOsale();
		vOsale.setCode(5);
		

		VOsale search = this.DAOsales.searchSaleByCode(vOsale);
		
		
		assertEquals(vOsale.getCode(),search.getCode());
	}

}
